package cn.stylefeng.guns.sys.modular.system.util;

import cn.stylefeng.guns.sys.core.properties.ZxUrlConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class ZxUrl {
    @Autowired
    private ZxUrlConfig zxUrlConfigure;

    @Autowired
    RestTemplate restTemplate;

    public String call(String clUrl,MultiValueMap map,String urls) {
        String url= clUrl+urls;
        String result;
        try {
            //因为在config配置中已经注入配置过了就不用再写了
            SimpleClientHttpRequestFactory requestFactory=new SimpleClientHttpRequestFactory();
            //设置超时
            requestFactory.setConnectTimeout(1000000);
            requestFactory.setReadTimeout(1000000);

            //利用复杂构造器可以实现超时设置，内部实际实现为 HttpClient
            RestTemplate restTemplate02=new RestTemplate(requestFactory);
            //设置请求头，设置编码等信息
            HttpHeaders httpHeaders=new HttpHeaders();
            httpHeaders.set("Accept-Charset", "utf-8");
            httpHeaders.set("Content-type", "text/xml; charset=utf-8");
            //这个是定义请求参数类型为form-data的
            httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);

            //设置请求体
            HttpEntity requestBody=new HttpEntity(map,httpHeaders);
            //设置响应体
            ResponseEntity<String> responseEntity=restTemplate02.postForEntity(url,requestBody,String.class);

            result=responseEntity.getBody();
        }catch (Exception e){throw e;}
        return result;
    }
}
